import { fakeAccountLogin } from '@/services/login';
import { setUserInfo } from '@/utils/index';

const Login = {
  namespace: 'login',

  state: {
    status: undefined,
    currentUser: {},
  },

  effects: {
    *login({ payload, callback }, { call, put }) {
      const response = yield call(fakeAccountLogin, payload);
      if (!response) {
        return;
      }
      yield put({
        type: 'changeLoginStatus',
        payload: response,
      });
      if (callback) {
        callback(response);
      }
    },
  },

  reducers: {
    changeLoginStatus(state, { payload }) {
      setUserInfo(payload);
      return {
        ...state,
        status: 'ok',
        type: 'account',
        currentUser: payload.user,
      };
    },
  },
};

export default Login;
